<template>
	<view class="content">
		<view class="header">
			<!-- v-for="(item, index) in list" -->
			<!-- :key="index" -->
			<!-- <zero-lazy-load
			    class="headerImg"
			    image="https://cdn.pixabay.com/photo/2020/05/19/13/32/cartoon-5190837_1280.jpg"
			    imgMode="aspectFill"
				height='406'
			  ></zero-lazy-load> -->
			<!-- <easy-loadimage mode="widthFix" class="headerImg" view-height='406' :scroll-top="0" loadingMode="spin-circle"
				image-src="https://cdn.pixabay.com/photo/2020/05/19/13/32/cartoon-5190837_1280.jpg"></easy-loadimage> -->
			<image src="https://cdn.pixabay.com/photo/2020/05/19/13/32/cartoon-5190837_1280.jpg" class="headerImg"
				mode="aspectFill" lazy-load></image>
			<view class="txtWrap">
				<image src="/static/index/selfPurchased.png" mode="aspectFit" class="pageTitleImg"></image>
				<view class="title">
					全职高手
				</view>
			</view>
			<view class="numberBox">
				<image class="numberIcon" src="/static/index/numberIcon2.png" mode="widthFix"></image>
				0/641
			</view>
		</view>
		<view class="cardWrap">
			<view class="topBox">
				<view class="title">
					再加购{{machine.gift_count}}张获得以下赠图
				</view>
				<view class="pictureBox">
					<view class="pictureItem" v-for="(item,index) in bonusList" :key="index" v-if="index==0">

						<cardBag :cardWidth='208' :cardHeight='208' :tagWidth='72' :cardData="item"
							:urlKey="'thumbnail'">
							<!-- <template #infoMore>
						<view class="dateBox">
							<template v-if="!item.disabled">
								<image src="/static/index/dateIcon.png" class="dateIcon" mode="aspectFit">
								</image>
								<view class="dateTxt">
									2025/5/29
								</view>
							</template>
						</view>
</template> -->
						</cardBag>
					</view>
				</view>
			</view>
			<view class="centerBox">
				<view class="pictureBox">
					<view class="pictureItem" v-for="(item,index) in directList" :key="index">

						<cardBag :cardWidth='208' :cardHeight='208' :tagWidth='72' :cardData="item"
							:urlKey="'thumbnail'">
							<template #option>
								<view class="optionLay" @click="toggleSelection(item)">
									<uni-icons class="optionIcon" :type="isSelected(item.id)?'checkbox-filled':'circle'"
										color="#fff" size="14"></uni-icons>
								</view>
							</template>
							<template #infoMore>
								<view class="infoMore">
									<view class="priceTxt ">
										<text class="unit">￥</text>
										{{item.direct_price}}
									</view>
									<view class="priceTxt forward">
										<view class="txt">
											转发价
										</view>
										<text class="unit">￥</text>
										{{item.forward_price}}
									</view>

								</view>
							</template>
						</cardBag>
					</view>
				</view>

			</view>
		</view>
		<view class="playWrap">
			<view class="all" @click="allIn">
				<uni-icons class="optionIcon" :type="allType?'checkbox-filled':'circle'" color="#3CC599"
					size="18"></uni-icons>
				打包
			</view>
			<view class="infoBox">
				<view class="infoTxt">
					<view class="lable">
						购买{{machine.gift_count}}张
					</view>
					<view class="value">
						<view class="lable">合计：</view>
						<view class="priceTxt ">
							<text class="unit">￥</text>
							{{totalPrice}}
						</view>
					</view>
				</view>
				<view class="infoTxt">
					<view class="lable">
						获赠{{bonusList.length}}张
					</view>
					<view class="value">
						<view class="lable">共减：</view>
						<view class="priceTxt ">
							<text class="unit">￥</text>
							<text class="delLin">
								{{selectedItems.length * machine.price}}</text>
						</view>
					</view>
				</view>
			</view>
			<view class="bnt" @click="ordersCreateFn(undefined,'lottery_purchase')">
				立即购买
			</view>
		</view>
	</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				allType: false,
				bonusList: [],
				directList: [],
				shopId: '',
				machineId: '',
				machine: {},
				maxSelectCount: 0, //可以进行自选直购的张数,最大选择个数，
				selectedItems: [],
				selectedIds: [],
				user_share: false,
				totalPrice: 0,
			};
		},
		onLoad(e) {
			// 移除用户选择的缓存数据
			/* uni.removeStorageSync('userSelects');
			uni.removeStorageSync('userSelectsIds'); */
			this.shopId = e.shopId;
			this.machineId = e.machineId;
			this.machinesDetailFn();
			// this.Toggle2() 
		},
		computed: {
			selectedMap() {
				const map = {};
				this.selectedIds.forEach(id => {
					map[id] = true;
				});
				return map;
			},
			isMaxReached() {
				return this.selectedIds.length >= this.maxSelectCount;
			}
		},
		methods: {
			allIn() {
				this.allType=!this.allType;
				this.directList.forEach(item=>{
					this.toggleSelection(item);
				})
			},
			optionFN(item) {
				item.action = !item.action
			},
			toggleSelection(item) {
				/* if (this.maxSelectCount==0) {
					uni.showToast({
						title: `自选直购张数不足`,
						icon: 'none'
					});
					return;
				} */
				if (this.isSelected(item.id)) {
					this.selectedIds = this.selectedIds.filter(id => id !== item.id);
					this.selectedItems = this.selectedItems.filter(selectedItem => selectedItem.id !== item.id);
					this.totalPriceFn();
					return;
				}

				// if (this.isMaxReached) {
				// 	uni.showToast({
				// 		title: `最多只能选择${this.maxSelectCount}个项目`,
				// 		icon: 'none'
				// 	});
				// 	return;
				// }

				this.selectedIds.push(item.id);
				this.selectedItems.push({
					...item
				});
				this.totalPriceFn();
			},
			isSelected(id) {
				return !!this.selectedMap[id];
			},
			isDisabled(id) {
				return this.isMaxReached && !this.isSelected(id);
			},
			// 计算价格
			totalPriceFn(){
				let totalPrice=0;
				if(this.selectedItems.length==0){
					this.totalPrice = totalPrice;
					return
				}
				// user_share == true?forward_price||direct_price
				this.selectedItems.forEach(item => {
					totalPrice += this.user_share ? item.forward_price : item.direct_price;
					item.price = this.user_share ? item.forward_price : item.direct_price;
				})
				this.totalPrice = totalPrice;
			},
			ordersCreateFn(num, type) {
				if (this.selectedIds.length === 0) {
					uni.showToast({
						title: '请至少选择一个',
						icon: 'none'
					});
					return;
				}
				// let newData = [this.list[0], this.list[1]]; //用户选择的图；
				console.log(this.selectedIds, this.selectedItems);
				uni.setStorageSync('userSelects', this.selectedItems)
				uni.setStorageSync('userSelectsIds', this.selectedIds)
				uni.navigateTo({
					url: `/pages/my/orderConfirmation?price=${this.totalPrice}&playType=purchase&shopId=${this.shopId}&machineId=${this.machineId}&userShare=${this.user_share}`
				})
			},
			// 通过ID获取机台详情
			machinesDetailFn() {
				this.$http('/v1/machines/detail/' + this.machineId).then(res => {
					console.log(res, "获取机台详情");
					if (res.code == 0) {
						const data = res.data;
						this.machine = data.machine;
						// this.maxSelectCount = data.draw_to_direct_count||0;
						// 直购图
						this.directList = data.all_images.filter(item => {
							return item.cat_type == 1;
						})
						// 增图
						this.bonusList = data.all_images.filter(item => {
							return item.cat_type == 2;
						})
						this.user_share = data.user_share;
						this.selectedItems = [];
						this.selectedIds = [];
					} else {
						this.$showToast(res.msg)
					}
				})

			},
		}
	}
</script>

<style lang="scss" scoped>
	.header {
		width: 100vw;
		height: 406upx;
		position: relative;

		::v-deep .headerImg {
			// image{
			@include setwh(100%, 100%);
			// }
		}

		.txtWrap {
			position: absolute;
			bottom: 86upx;
			left: 50upx;
			@include flexLayout(center, center);
			flex-direction: column;

			.pageTitleImg {
				@include setwh(222upx, 40upx);
				margin-bottom: 6upx;

			}

			.title {
				font-weight: normal;
				font-size: 24rpx;
				color: #FFFFFF;
				line-height: 32rpx;
			}
		}

		.numberBox {
			margin-left: 28upx;
			min-width: 106rpx;
			height: 36upx;
			border: 2upx solid #FFFFFF;
			border-radius: 20upx;
			padding: 0 10upx;
			@include flexLayout(center, center);
			font-family: SourceHanSansSC, SourceHanSansSC;
			font-weight: bold;
			font-size: 22rpx;
			line-height: 1;
			color: #FFFFFF;
			position: absolute;
			bottom: 104upx;
			right: 60upx;

			.numberIcon {
				@include setwh(26rpx, 0);
				padding-right: 12upx;
			}
		}
	}

	.cardWrap {
		width: 100%;
		min-height: calc(100vh - 406upx);
		position: relative;
		top: -48upx;
		background: #FFFFFF;
		// box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(0,0,0,0.5);
		border-radius: 29rpx 29rpx 0rpx 0rpx;

		.topBox {

			.title {
				padding-top: 48upx;
				padding-left: 36upx;
				margin-bottom: 32upx;
				font-family: SourceHanSansSC, SourceHanSansSC;
				font-weight: 400;
				font-size: 29rpx;
				color: #000000;
				line-height: 40rpx;
			}
		}

		.pictureBox {
			// width: 600upx;
			margin-left: 20upx;
			margin-top: 20upx;
			@include flexLayout(center, flex-start);
			flex-wrap: wrap;
			column-gap: 8rpx;
			row-gap: 28upx;
			// background: #FFFFFF;
			position: relative;
			z-index: 3;

			.optionLay {
				position: absolute;
				top: 0;
				left: 0;
				width: 40rpx;
				height: 32rpx;
				background: rgba(0, 0, 0, 0.43);
				border-radius: 0rpx 0rpx 10rpx 0rpx;

				.optionIcon {
					@include positionCenter;
					line-height: 1;
				}
			}

			.infoMore {
				@include flexLayout(flex-end, space-between);
				padding-top: 11upx;
				min-height: 28upx;

				.priceTxt {
					@include flexLayout(center);
					font-family: SourceHanSansSC, SourceHanSansSC;
					font-weight: bold;
					font-size: 24rpx;
					color: #FF7994;

					// line-height: 32rpx;
					.unit {
						font-size: 18rpx;
						// line-height: 1;
					}
				}

				.forward {
					color: #828282;
					font-size: 14rpx;

					.txt {
						margin-right: 6upx;
					}

					.unit {
						font-size: 16rpx;
						// line-height: 1;
					}
				}
			}
		}

		.centerBox {
			margin-top: 82upx;
			padding-bottom: 200upx;
		}
	}

	.playWrap {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		width: 750rpx;
		height: 187rpx;
		background: #FFFFFF;
		box-shadow: 0rpx -4rpx 29rpx 4rpx rgba(210, 210, 210, 0.5);
		border-radius: 29rpx 29rpx 0rpx 0rpx;
		@include flexLayout(center, space-between);
		padding: 32upx 58upx 70upx 48upx;
		z-index: 9;

		.all {
			font-family: SourceHanSansSC, SourceHanSansSC;
			font-weight: 500;
			font-size: 24rpx;
			color: #656565;
			line-height: 32rpx;

			.optionIcon {
				margin-right: 18upx;
			}
		}

		.infoBox {
			.infoTxt {
				@include flexLayout(center, flex-start);

				.lable {
					font-family: SourceHanSansSC, SourceHanSansSC;
					font-weight: 500;
					font-size: 24rpx;
					color: #656565;
					line-height: 32rpx;
				}

				.value {
					@include flexLayout(center, center);
					margin-left: 38upx;

					.priceTxt {
						font-family: SourceHanSansSC, SourceHanSansSC;
						font-weight: bold;
						font-size: 32rpx;
						color: #FF7994;
						line-height: 48rpx;

						.unit {
							font-size: 34rpx;
						}

						.delLin {
							position: relative;

							&::before {
								content: '';
								@include positionCenter;
								background-color: #FF7994;
								width: 100%;
								height: 4upx;
							}
						}
					}
				}
			}
		}

		.bnt {
			width: 144rpx;
			height: 56rpx;
			background: #FF7994;
			border-radius: 32rpx;
			font-family: SourceHanSansSC, SourceHanSansSC;
			font-weight: 500;
			font-size: 24rpx;
			color: #FFFFFF;
			line-height: 56rpx;
			text-align: center;
		}
	}
</style>